package com.xuexiang.xqrcode.util;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.Result;
import com.google.zxing.common.HybridBinarizer;
import com.xuexiang.xqrcode.camera.BitmapLuminanceSource;
import com.xuexiang.xqrcode.decoding.DecodeFormatManager;
import java.io.File;
import java.util.Hashtable;
import java.util.Vector;

/* loaded from: classes2.dex */
public final class QRCodeAnalyzeUtils {

    /* loaded from: classes2.dex */
    public interface AnalyzeCallback {
        void onAnalyzeFailed();

        void onAnalyzeSuccess(Bitmap bitmap, String str);
    }

    private QRCodeAnalyzeUtils() {
        throw new UnsupportedOperationException("Do not need instantiate!");
    }

    public static void analyze(String str, AnalyzeCallback analyzeCallback) {
        Bitmap qRCodeBitmap = getQRCodeBitmap(str);
        Result analyze = analyze(qRCodeBitmap);
        if (analyze != null) {
            if (analyzeCallback != null) {
                analyzeCallback.onAnalyzeSuccess(qRCodeBitmap, analyze.getText());
            }
        } else if (analyzeCallback != null) {
            analyzeCallback.onAnalyzeFailed();
        }
    }

    public static String analyze(String str) {
        Result analyzeQRCodeResult = getAnalyzeQRCodeResult(str);
        return analyzeQRCodeResult != null ? analyzeQRCodeResult.getText() : "";
    }

    public static Result getAnalyzeQRCodeResult(String str) {
        return analyze(getQRCodeBitmap(str));
    }

    private static Bitmap getQRCodeBitmap(String str) {
        return getBitmap(str, 400, 400);
    }

    public static Result analyze(Bitmap bitmap) {
        MultiFormatReader multiFormatReader = new MultiFormatReader();
        Hashtable hashtable = new Hashtable(2);
        Vector vector = new Vector();
        if (vector.isEmpty()) {
            vector = new Vector();
            vector.addAll(DecodeFormatManager.ONE_D_FORMATS);
            vector.addAll(DecodeFormatManager.QR_CODE_FORMATS);
            vector.addAll(DecodeFormatManager.DATA_MATRIX_FORMATS);
        }
        hashtable.put(DecodeHintType.POSSIBLE_FORMATS, vector);
        multiFormatReader.setHints(hashtable);
        try {
            return multiFormatReader.decodeWithState(new BinaryBitmap(new HybridBinarizer(new BitmapLuminanceSource(bitmap))));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Bitmap getBitmap(String str, int i, int i2) {
        if (isFileExists(str)) {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(str, options);
            options.inSampleSize = calculateInSampleSize(options, i, i2);
            options.inJustDecodeBounds = false;
            return BitmapFactory.decodeFile(str, options);
        }
        return null;
    }

    private static int calculateInSampleSize(BitmapFactory.Options options, int i, int i2) {
        int i3 = options.outHeight;
        int i4 = options.outWidth;
        int i5 = 1;
        while (true) {
            i4 >>= 1;
            if (i4 < i || (i3 = i3 >> 1) < i2) {
                break;
            }
            i5 <<= 1;
        }
        return i5;
    }

    private static File getFileByPath(String str) {
        if (isSpace(str)) {
            return null;
        }
        return new File(str);
    }

    private static boolean isFileExists(String str) {
        return isFileExists(getFileByPath(str));
    }

    private static boolean isFileExists(File file) {
        return file != null && file.exists();
    }

    private static boolean isSpace(String str) {
        if (str == null) {
            return true;
        }
        int length = str.length();
        for (int i = 0; i < length; i++) {
            if (!Character.isWhitespace(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }
}
